package com.situ.day1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.mysql.jdbc.PreparedStatement;
import com.situ.day7.Insect;

public class ZSG {
	@Test   //删、改、增加
	public void test1() {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			// 1、加载驱动：Class.forName("");
			Class.forName("com.mysql.jdbc.Driver");
			// 2、获得链接对象Connection
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java1807", "root", "Root");//数据库名
			// 3、写sql语句
			String sql = "insert into Student (name,age,gender) values ('张三',5,'女')";//添加一行
//			String sql = "DELETE FROM Student WHERE id=17;"; //删除id为17的一行
			// 4、创建Statement
			statement = connection.createStatement();
			// 5、执行sql语句
			// 更新：delete/update/insert executeUpdate() 返回值int：表示影响的行数.
			// 查询：select executeQuery() 返回结果集：ResultSet
//			resultSet = statement.executeQuery(sql);
			// System.out.println(resultSet);
			int count = statement.executeUpdate(sql);
			System.out.println("count" + count);
			
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			// 6、关闭连接
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	//实验终极版
	@Test
	public void testUpdate() {
	   Connection connection = null;
	   Statement statement = null;
	   ResultSet resultSet = null;
	   try {
	     //2、获得连接对象Connection
	     connection = JDBCUtilEnd.getConnection();
	     //3、写sql语句
	     //String sql = "update student set age=age+8 where id=1;";
	     //String sql = "insert into student(name,age,gender) values('yyyy',30,'男');";
	     String sql = "select id,name,age,gender from student;";
	     //4、创建Statement
	     statement = connection.createStatement();
//	     PreparedStatement
//	           预 编译 、效率高、防止sql注入
	     //5、执行sql语句
	     //     更新类(更新了表里面数据)：delete/update/insert      executeUpdate()   返回值int:表示你影响的行数

	     //     查询(没有改变表里面数据)：select                    executeQuery()     返回结果集：ResultSet
	     resultSet = statement.executeQuery(sql);
	     List<Student> list = new ArrayList<>();
	     while (resultSet.next()) {
	        Integer id = resultSet.getInt("id");
	        String name = resultSet.getString("name");
	        Integer age = resultSet.getInt("age");
	        String gender = resultSet.getString("gender");
	        Student student = new Student(id, name, age, gender);
	        list.add(student);
	     }    
	     for (Student student : list) {
	         System.out.println(student);
	      }
	   } catch (SQLException e) {
	     e.printStackTrace();
	   } finally {
	     //6、关闭连接
	      JDBCUtilEnd.close(connection, statement, null);
	   }

	}
	
}

